<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改菜单')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-menu-edit" th:object="${menu}">
			<input id="menuId" name="id"   type="hidden" th:field="*{id}"/>
			<input id="treeId" name="parentId" type="hidden" th:field="*{parentId}" />
			<div class="form-group">
				<label class="col-sm-3 control-label ">上级菜单：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" onclick="selectMenuTree()" id="treeName" readonly="readonly" th:value="${parent.menuName}">
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单类型：</label>
				<div class="col-sm-8">
					<input type="hidden" name="menuType"  th:field="*{menuType}" >
					<div class="radio-box" th:each="dict : ${@dict.getDict('sys_menu_type')}">
						<input type="radio" th:id="${dict.id}" name="menuType" th:value="${dict.dictValue}" th:field="*{menuType}" disabled="disabled" >
						<label th:for="${dict.id}" th:text="${dict.dictName}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getDict('sys_show_hide')}">
						<input type="radio" th:id="${dict.id}" name="visible" th:value="${dict.dictValue}" th:field="*{visible}">
						<label th:for="${dict.id}" th:text="${dict.dictName}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="menuName" id="menuName" th:field="*{menuName}" required>
				</div>
			</div>
			<div class="form-group" style="display: none;">
				<label class="col-sm-3 control-label">请求地址：</label>
				<div class="col-sm-8">
					<input id="url" name="url" class="form-control" type="text" th:field="*{url}">
				</div>
			</div>
			<div class="form-group" style="display: none;">
				<label class="col-sm-3 control-label">打开方式：</label>
				<div class="col-sm-8">
					<label class="radio-box"> <input type="radio" name="target" value="menuItem" th:checked="*{target == 'menuItem'}"/> 页签 </label> 
					<label class="radio-box"> <input type="radio" name="target" value="menuBlank" th:checked="*{target == 'menuBlank'}" /> 窗口</label>
				</div>
			</div>
			<div class="form-group" style="display: none;">
				<label class="col-sm-3 control-label">权限标识：</label>
				<div class="col-sm-8">
					<input id="perms" name="perms" class="form-control" type="text" th:field="*{perms}">
				    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 权限标识使用如：@permission.hasPerms("module:entity:key")</span>
				</div>
			</div>
			<div class="form-group" style="display: none;">
				<label class="col-sm-3 control-label">图标：</label>
				<div class="col-sm-8">
					<input id="icon" name="icon" class="form-control" type="text" placeholder="选择图标" th:field="*{icon}">
                    <div class="ms-parent" style="width: 100%;">
                        <div class="icon-drop animated flipInX" style="display: none;max-height:200px;overflow-y:auto">
                            <div data-th-include="system/menu/icon"></div>
                        </div>
                    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">显示顺序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="number" name="orderNum" th:field="*{orderNum}" required="required">
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	 <script>
        var prefix = ctx + "system/menu";
        
        $(function() {
            $("input[name='icon']").focus(function() {
                $(".icon-drop").show();
            });
            $("#form-menu-edit").click(function(event) {
                var obj = event.srcElement || event.target;
                if (!$(obj).is("input[name='icon']")) {
                    $(".icon-drop").hide();
                }
            });
            $(".icon-drop").find(".ico-list i").on("click", function() {
                $('#icon').val($(this).attr('class'));
            });
            $('input').on('ifChecked', function(event) {
                var menuType = $(event.target).val();
                menuVisible(menuType);
            });
            
            var menuType = $('input[name="menuType"]:checked').val();
            menuVisible(menuType);
        });

        $("#form-menu-edit").validate({
        	onkeyup: false,
        	rules:{
        		menuType:{
        			required:true,
        		},
        		menuName:{
        			remote: {
                        url: prefix + "/checkMenuNameUnique",
                        type: "get",
                        dataType: "json",
                        data: {
                        	id: function() {
                                return $("#menuId").val();
                            },
                            parentId: function() {
		                		return $("#treeId").val();
		                    },
		                    menuName: function() {
                                return $.common.trim($("#menuName").val());
                            }
                        },
                        dataFilter: function(data, type) {
                        	return $.validate.unique(data);
                        }
                    }
        		},
        		orderNum:{
        			digits:true
        		},
        	},
        	messages: {
        		menuName: {
                    remote: "菜单已经存在"
                }
            },
            focusCleanup: true
        });
        
        function submitHandler() {
	        if ($.validate.form()) {
	            $.operate.save(prefix + "/edit", $('#form-menu-edit').serialize());
	        }
	    }

        function menuVisible(menuType) {
        	if (menuType == "catalog") {
                $("input[name='url']").parents(".form-group").hide();
                $("input[name='target']").parents(".form-group").hide();
                $("input[name='perms']").parents(".form-group").hide();
                $("input[name='icon']").parents(".form-group").show();
            } else if (menuType == "menu") {
            	$("input[name='url']").parents(".form-group").show();
            	$("input[name='target']").parents(".form-group").show();
                $("input[name='perms']").parents(".form-group").show();
                $("input[name='icon']").parents(".form-group").hide();
            } else if (menuType == "button") {
            	$("input[name='url']").parents(".form-group").hide();
            	$("input[name='target']").parents(".form-group").hide();
                $("input[name='perms']").parents(".form-group").show();
                $("input[name='icon']").parents(".form-group").hide();
            }
        }

        /*菜单管理-修改-选择菜单树*/
        function selectMenuTree() {
        	var parentId = $("#treeId").val();
       		var url = prefix + "/selectMenuTree/" + parentId;
       		var options = {
  				title: '菜单选择',
  				width: "400",
  				url: url,
  				callBack: function(index, layero){
  					var body = layer.getChildFrame('body', index);
  		   			$("#treeId").val(body.find('#treeId').val());
  		   			$("#treeName").val(body.find('#treeName').val());
  		   			layer.close(index);
  				}
  			};
  			$.modal.openOptions(options);
		}
		
    </script>
</body>
</html>
